package gqz.mvvm.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import gqz.mvvm.R;

/**
 * Created by Administrator on 2018/3/10.
 */

public class GlideActivity extends Activity implements View.OnClickListener {

    private String TAG = "GlideActivity-->";

    private Button btn1, btn2, btn3, btn4;
    private ImageView iv1, iv2, iv3, iv4, iv5, iv6;
    private ImageView[] ivs = new ImageView[6];
    private int[] drawables = {R.drawable.img_4k_1, R.drawable.img_4k_2, R.drawable.img_4k_3,
            R.drawable.img_4k_4, R.drawable.img_4k_5, R.drawable.img_4k_6};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide);
        initView();
    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.glide_btn1);
        btn2 = (Button) findViewById(R.id.glide_btn2);
        btn3 = (Button) findViewById(R.id.glide_btn3);
        btn4 = (Button) findViewById(R.id.glide_btn4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        iv1 = (ImageView) findViewById(R.id.glide_img1);
        iv2 = (ImageView) findViewById(R.id.glide_img2);
        iv3 = (ImageView) findViewById(R.id.glide_img3);
        iv4 = (ImageView) findViewById(R.id.glide_img4);
        iv5 = (ImageView) findViewById(R.id.glide_img5);
        iv6 = (ImageView) findViewById(R.id.glide_img6);
        ivs[0] = iv1;
        ivs[1] = iv2;
        ivs[2] = iv3;
        ivs[3] = iv4;
        ivs[4] = iv5;
        ivs[5] = iv6;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.glide_btn1:
                iv1.setImageDrawable(getResources().getDrawable(R.drawable.img_4k_2));
                break;
            case R.id.glide_btn2:
                Glide.with(getApplicationContext()).load(R.drawable.img_4k_2).into(iv1);
                break;
            case R.id.glide_btn3://OOM
                for (int i = 0; i < ivs.length; i++) {
                    Log.i(TAG, "i=" + i);
                    ivs[i].setImageDrawable(getResources().getDrawable(drawables[i]));
                }
                break;
            case R.id.glide_btn4:
                for (int i = 0; i < ivs.length; i++) {
                    Glide.with(getApplicationContext())
                            .load(drawables[i])//加载图片
//                            .placeholder(R.mipmap.ic_launcher)//设置占位图，加载之前显示的图片
                            .error(R.mipmap.icon_false)//加载失败图
                            .skipMemoryCache(true)//禁止内存缓存
                            .diskCacheStrategy(DiskCacheStrategy.NONE)//禁止硬盘缓存
                            .crossFade(1000)//载入动画
                            .override(1000,1000)//缩放
                            .thumbnail(0.1f)//先显示缩略图
//                            .centerCrop()
//                            .fitCenter()
                            .into(ivs[i]);//显示控件
                }
                break;
        }
    }
}
